package com.hyperboost.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import com.hyperboost.constant.MessageConstant;
import com.hyperboost.dto.ShareDTO;
import com.hyperboost.dto.SharePageQueryDto;
import com.hyperboost.entity.FrontUser;
import com.hyperboost.entity.Share;
import com.hyperboost.mapper.ShareMapper;
import com.hyperboost.mapper.TagMapper;
import com.hyperboost.mapper.UserMapper;
import com.hyperboost.result.PageResult;
import com.hyperboost.result.Result;
import com.hyperboost.service.ShareService;
import com.hyperboost.utils.ThreadLocalUtil;
import com.hyperboost.vo.ShareCommentReplyVO;
import com.hyperboost.vo.ShareVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Transactional
@Service
@Slf4j
public class ShareServiceImpl implements ShareService {
    @Autowired
    private ShareMapper shareMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PageHelperProperties pageHelperProperties;
    @Autowired
    private View view;

    /**
     * 添加分享
     *
     * @param shareDTO
     */
    @Override
    @Transactional
    public void shareAdd(ShareDTO shareDTO) {
        //获取当前用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
        //根据用户id查询用户信息
        FrontUser userByUserId = userMapper.getUserByUserId(userId);
        //提取创建人
        String createBy = userByUserId.getUsername();
        //设置创建人
        shareDTO.setCreateBy(createBy);
        //设置用户id
        shareDTO.setUserId(userId);
        //添加分享
        shareMapper.addShare(shareDTO);
        //查询分享id
        Long shareId = shareMapper.selectLastId();
        //设置分享id
        shareDTO.setShareId(shareId);
        //添加标签
        tagMapper.addTag(shareDTO);

    }

    /**
     * 删除分享
     *
     * @param shareId
     */
    @Transactional
    @Override
    public Result deleteShare(Long shareId) {
        //获取当前用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
     Long userIdByShareId =shareMapper.getUserIdByShareId(shareId);
     if (userId == userIdByShareId) {
        //删除分享
        shareMapper.deleteShare(shareId);
        //删除标签
        tagMapper.deleteTag(shareId);
        return Result.success("分享删除成功");
     }else
         return Result.error("你无权删除他人的分享");
    }

    /**
     * 分页查询分享
     *
     * @param sharePageQueryDto
     * @return
     */
    @Override
    public PageResult<ShareVo> pageShare(SharePageQueryDto sharePageQueryDto) {
        //new一个返回结果对象
        PageResult<ShareVo> pageResult = new PageResult<>();
        //用pahehelper分页查询
        PageHelper.startPage(sharePageQueryDto.getPageNum(), sharePageQueryDto.getPageSize());
        //查询所有分享
        List<ShareVo> shares = shareMapper.getAllPageShare(sharePageQueryDto);
        //注入查询总数
        pageResult.setTotal(shares.size());
        //注入查询结果
        pageResult.setRecords(shares);
        return pageResult;
    }

    /**
     * 分页查询用户自己发布的分享
     *
     * @param sharePageQueryDto
     * @return
     */
    @Override
    public PageResult<ShareVo> pageShareByUserId(SharePageQueryDto sharePageQueryDto) {
        PageResult<ShareVo> pageResult = new PageResult<>();
        //获取当前用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
        //用pahehelper分页查询
        PageHelper.startPage(sharePageQueryDto.getPageNum(), sharePageQueryDto.getPageSize());
        List<ShareVo> shares = shareMapper.getAllPageShareByUserId(sharePageQueryDto, userId);
        //注入查询分享总数
        pageResult.setTotal(shares.size());
        //注入查询分享结果
        pageResult.setRecords(shares);
        return pageResult;
    }

    /**
     * 根据标签Id查询分享
     *
     * @param noticeId
     * @return
     */
    @Override
    public List<ShareVo> getShareByNoticeId(Long noticeId) {
        List<ShareVo> shareVos = shareMapper.getShareByNoticeId(noticeId);
        return shareVos;


    }

    /**
     * 根据分享id查询分享,并浏览数加1
     *
     * @param shareId
     * @return
     */
    @Override
    public ShareVo getShareByShareId(Long shareId) {
        shareMapper.getViewCountByShareId(shareId);
        ShareVo shareVo = shareMapper.getShareByShareId(shareId);
        return shareVo;
    }


}
